<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="联系状态" prop="contactStatus">
        <el-select v-model="queryParams.contactStatus" placeholder="请选择联系状态" clearable size="small">
          <el-option label="接通" value="connected" />
          <el-option label="未接通" value="notConnected" />
        </el-select>
      </el-form-item>
      
      <el-form-item label="导入时间" prop="importTime">
        <el-date-picker
          v-model="queryParams.importTime"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    
    <el-table v-loading="loading" :data="leadsList">
      <el-table-column label="姓名" align="center" prop="name" />
      <el-table-column label="手机号" align="center" prop="phone" />
      <el-table-column label="地址" align="center" prop="address" />
      <el-table-column label="导入时间" align="center" prop="importTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.importTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="联系状态" align="center" prop="contactStatus">
        <template slot-scope="scope">
          <el-select 
            v-model="scope.row.contactStatus" 
            @change="handleStatusChange(scope.row)" 
            size="mini"
            style="width: 100px"
          >
            <el-option label="接通" value="connected" />
            <el-option label="未接通" value="notConnected" />
          </el-select>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            type="primary"
            @click="handleOrder(scope.row)"
          >下单</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    
    <!-- 下单弹窗 -->
    <el-dialog title="下单" :visible.sync="orderFormVisible" width="500px" append-to-body>
      <el-form ref="orderForm" :model="orderForm" :rules="orderRules" label-width="80px">
        <el-form-item label="客户姓名" prop="name">
          <el-input v-model="orderForm.name" disabled />
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="orderForm.phone" disabled />
        </el-form-item>
        <el-form-item label="药品" prop="productId">
          <el-select 
            v-model="orderForm.productId" 
            filterable 
            remote 
            :remote-method="searchProducts"
            :loading="false"
            placeholder="请选择药品"
            style="width: 100%">
            <el-option
              v-for="item in productOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id">
              <span style="float: left">{{ item.name }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ item.category }}</span>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="数量" prop="quantity">
          <el-input-number v-model="orderForm.quantity" :min="1" :max="999" style="width: 100%" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitOrder">确 定</el-button>
        <el-button @click="cancelOrder">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

    </style>

<script>
import { parseTime } from "@/utils/ruoyi";

export default {
  name: "LeadsInfo",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 线索表格数据
      leadsList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        contactStatus: undefined,
        importTime: []
      },
      // 下单表单可见性
      orderFormVisible: false,
      // 下单表单数据
      orderForm: {
        leadId: undefined,
        name: '',
        phone: '',
        productId: undefined,
        quantity: 1
      },
      // 药品选项
      productOptions: [],
      // 表单规则
      orderRules: {
        productId: [{ required: true, message: '请选择药品', trigger: 'change' }],
        quantity: [{ required: true, message: '请输入数量', trigger: 'blur' }]
      }
    };
  },
  created() {
    this.getList();
    this.getProductOptions();
  },
  methods: {
    /** 查询线索列表 */
    getList() {
      this.loading = true;
      // 使用mock数据
      this.leadsList = this.getMockData();
      this.total = this.leadsList.length;
      this.loading = false;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 状态修改 */
    handleStatusChange(row) {
      // 这里应该调用API更新状态，目前使用mock
      this.$message.success(`"${row.name}" 的联系状态已更新为 "${row.contactStatus === 'connected' ? '接通' : '未接通'}"`);
    },
    /** 下单按钮操作 */
    handleOrder(row) {
      this.orderForm.leadId = row.id;
      this.orderForm.name = row.name;
      this.orderForm.phone = row.phone;
      this.orderForm.productId = undefined;
      this.orderForm.quantity = 1;
      this.orderFormVisible = true;
    },
    /** 提交下单 */
    submitOrder() {
      this.$refs.orderForm.validate(valid => {
        if (valid) {
          // 这里应该调用API提交订单，目前使用mock
          const product = this.productOptions.find(p => p.id === this.orderForm.productId);
          this.$message.success(`为 "${this.orderForm.name}" 下单成功！药品：${product.name}，数量：${this.orderForm.quantity}`);
          this.orderFormVisible = false;
        }
      });
    },
    /** 取消下单 */
    cancelOrder() {
      this.orderFormVisible = false;
      this.$refs.orderForm.resetFields();
    },
    /** 获取药品选项 */
    getProductOptions() {
      // 使用mock数据
      this.productOptions = this.getMockProducts();
    },
    /** 时间格式化 */
    parseTime(time, format) {
      return parseTime(time, format);
    },
    /** 获取mock数据 */
    getMockData() {
      return [
        {
          id: 1,
          name: "张三",
          phone: "13800138000",
          address: "北京市朝阳区",
          importTime: "2025-10-20",
          contactStatus: "notConnected"
        },
        {
          id: 2,
          name: "李四",
          phone: "13900139000",
          address: "上海市浦东新区",
          importTime: "2025-10-21",
          contactStatus: "connected"
        },
        {
          id: 3,
          name: "王五",
          phone: "13700137000",
          address: "广州市天河区",
          importTime: "2025-10-22",
          contactStatus: "notConnected"
        },
        {
          id: 4,
          name: "赵六",
          phone: "13600136000",
          address: "深圳市南山区",
          importTime: "2025-10-23",
          contactStatus: "connected"
        },
        {
          id: 5,
          name: "钱七",
          phone: "13500135000",
          address: "杭州市西湖区",
          importTime: "2025-10-24",
          contactStatus: "notConnected"
        }
      ];
    },
    /** 获取mock药品数据 */
    getMockProducts() {
      return [
        { id: 1, name: "感冒灵颗粒", category: "感冒药" },
        { id: 2, name: "布洛芬缓释胶囊", category: "止痛药" },
        { id: 3, name: "维生素C片", category: "营养补充剂" },
        { id: 4, name: "奥美拉唑肠溶胶囊", category: "胃药" },
        { id: 5, name: "阿莫西林胶囊", category: "抗生素" },
        { id: 6, name: "板蓝根颗粒", category: "清热解毒药" },
        { id: 7, name: "复方甘草片", category: "止咳药" },
        { id: 8, name: "硝苯地平缓释片", category: "降压药" }
      ];
    }
  }
};
</script>